using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// ui视图基类
/// </summary>
public abstract class UiView : MonoBehaviour
{

    /// <summary>
    /// 画布
    /// </summary>
    public Canvas canvas;

    /// <summary>
    /// UI预制体
    /// </summary>
    public GameObject viewGameObject;

    /// <summary>
    /// 配置
    /// </summary>
    private UiConfig uiConfig;

    public int SelfViewId
    {
        get
        {
            if (uiConfig!=null)
            {
                return uiConfig.viewId;
            }
            return 0;
        }
    }

    /// <summary>
    /// 图层类型
    /// </summary>
    public int LayerType
    {
        get
        {
            if (uiConfig!=null)
            {
                return uiConfig.layerType;
            }
            return UiConfigManger.None;
        }
    }

    /// <summary>
    /// 图层排序
    /// </summary>
    public int SortingOrder
    {
        get
        {
            if (canvas!=null)
            {
                return canvas.sortingOrder;
            }
            return 0;
        }
        set
        {
            if (canvas!=null)
            {
                canvas.sortingOrder = value;
            }
        }
    }

    /// <summary>
    /// 是否为基础Ui
    /// </summary>
    public bool IsLarget
    {
        get
        {
            if (uiConfig!=null)
            {
                return uiConfig.isLarge;
            }
            return false;
        }
    }

    protected RectTransform canvasRectTransform;
    protected RectTransform viewRectTransform;

    protected void InnerInitialize()
    {

        if (canvasRectTransform==null&&canvas!=null)
        {
            canvasRectTransform = canvas.GetComponent<RectTransform>();
        }
        if (viewRectTransform==null&&viewGameObject!=null)
        {
            viewRectTransform = viewGameObject.GetComponent<RectTransform>();
        }
    }

    public virtual void Awake()
    {
        InnerInitialize();
        //UpdateBangHeight();
    }




    public void SetUIConfig(UiConfig uiConfig)
    {
        this.uiConfig = uiConfig;
        OnLoad();
    }

    public virtual void OnLoad()
    {
        if (canvas==null)
        {
            canvas = GetComponent<Canvas>();
        }

        if (uiConfig!=null)
        {
            canvas.sortingLayerName = "Ui";
            canvas.sortingOrder = uiConfig.layerType;
        }
    }

    public virtual void OnShow()
    {
        SetAcitve(true);
    }

    /// <summary>
    /// 
    /// </summary>
    public virtual void OnHide()
    {
        SetAcitve(false);
    }

    /// <summary>
    /// 删除
    /// </summary>
    public virtual void OnRemove()
    {
        Destroy(gameObject);
    }

    /// <summary>
    /// 设置激活或关闭
    /// </summary>
    /// <param name="active"></param>
    public void SetAcitve(bool active)
    {
        gameObject.SetActive(active);
        //if (active)
        //{
        //    OnShow();
        //}
        //else
        //{
        //    OnHide();
        //}
    }

    /// <summary>
    /// 设置激活或关闭
    /// </summary>
    /// <param name="active"></param>
    public void SetViewAcitve(bool active)
    {
        if (viewGameObject!=null)
        {
            viewGameObject.SetActive(active);
        }
    }

    /// <summary>
    /// 获取ViewID
    /// </summary>
    /// <returns></returns>
    public int GetViewId()
    {
        if (uiConfig!=null)
        {
            return uiConfig.viewId;
        }

        return 0;
    }

    /// <summary>
    /// 清除自身
    /// </summary>
    public void RemoveSelf()
    {
        GameUi.Instance.RemoveView(this);
    }


    #region 必须实现的部分

    /// <summary>
    /// 设置模块
    /// </summary>
    /// <param name="gameModule"></param>
    public abstract void SetModule(GameModule gameModule);

    /// <summary>
    /// 设置模块数据
    /// </summary>
    /// <param name="modelData"></param>
    public abstract void SetModuleData(System.Object modelData);

    /// <summary>
    /// 初始化
    /// </summary>
    public abstract void Initialize();

    /// <summary>
    /// 更新Ui
    /// </summary>
    public abstract void UpdateView();


    #endregion



    ///// <summary>
    ///// 
    ///// </summary>
    //public void UpdateBangHeight()
    //{
    //    if (viewRectTransform!=null)
    //    {
    //        int bangHeight = GetBangHeight();
    //        //����ƫ��
    //        viewRectTransform.offsetMax -= new Vector2(0, bangHeight);
    //    }
    //}

    ///// <summary>
    ///// ��ȡ�ֻ���Ļ�����ĸ߶ȣ��Ա����UI�߶�
    ///// </summary>
    ///// <returns></returns>
    //public int GetBangHeight()
    //{
    //    if (canvasRectTransform!=null&&viewRectTransform!=null)
    //    {
    //        int banHeight = Screen.height - (int)Screen.safeArea.height;
    //        return banHeight;
    //    }
    //    return 0;
    //}

}
